package com.binary.coder.gift.core.application.Invitation;

import cn.dev33.satoken.stp.StpUtil;
import com.binary.coder.gift.core.common.bean.PageBean;
import com.binary.coder.gift.core.domain.invitation.AppInvitationService;
import com.binary.coder.gift.core.domain.invitation.model.aggregate.HandleInvitationRecordAggregate;
import com.binary.coder.gift.core.domain.invitation.model.aggregate.InvitationCardAggregate;
import com.binary.coder.gift.core.domain.invitation.InvitationService;
import com.binary.coder.gift.core.domain.invitation.model.entity.AppInvitationCardEntity;
import com.binary.coder.gift.core.domain.invitation.model.entity.InvitationEntrustEntity;
import com.binary.coder.gift.core.domain.invitation.model.entity.InvitationRecordEntity;
import com.binary.coder.gift.core.domain.invitation.repository.InvitationRepository;
import com.binary.coder.gift.core.interfaces.Invitation.assembler.InvitationCardAssembler;
import com.binary.coder.gift.core.interfaces.Invitation.dto.req.*;
import com.binary.coder.gift.core.interfaces.Invitation.dto.res.QueryHandleInvitationRecordRes;
import com.binary.coder.gift.core.interfaces.Invitation.dto.res.QueryInvitationEntrustRes;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author atao
 * @version 1.0.0
 * @ClassName InvitationApplication.java
 * @Description 请帖应用
 * @createTime 2024年06月21日 16:56:00
 */
@Component
public class InvitationApplication {

    @Resource
    private InvitationService invitationService;

    @Resource
    private InvitationRepository invitationRepository;

    @Resource
    private AppInvitationService appInvitationService;

    public void saveAppInvitationCard(AppInvitationCardEntity req) {
        // 保存请帖
        appInvitationService.saveAppInvitation(req);
    }

    public void saveInvitationCard(InvitationCardAggregate invitationCardAggregate) {
        // 保存请帖
        invitationService.saveInvitation(invitationCardAggregate);
    }

    public boolean deleteInvitationCardById(long userId,long invitationCardId) {
        // 检查权限
        boolean isAllow = invitationService.checkPermission(userId,invitationCardId);
        if (isAllow) {
            // 删除请帖
            return invitationService.deleteInvitationCardById(invitationCardId);
        }
        return false;
    }


    public PageBean<InvitationCardAggregate> pageQueryInvitationCard(PageQueryInvitationCardReq req) {
        return invitationService.pageQueryInvitationCard(req);
    }

    public void handleInvitationCard(InvitationRecordEntity entity) {
        // 新增一条邀请处理记录
        invitationService.saveInvitationRecord(entity);
    }

    public HandleInvitationRecordAggregate queryInvitationRecord(Long userId, Long invitationCardId) {
        // 查询请帖
        InvitationCardAggregate invitationCardAggregate = invitationService.queryInvitationCard(invitationCardId);
        // 查询记录
        HandleInvitationRecordAggregate invitationRecordAggregate = invitationService.queryInvitationRecord(userId,invitationCardId);
        // 组装返回
        invitationRecordAggregate.setInvitationCardAggregate(invitationCardAggregate);
        return invitationRecordAggregate;
    }

    public PageBean<HandleInvitationRecordAggregate> pageQueryInvitationRecord(PageQueryInvitationRecordReq req) {
        // 查询请帖
        InvitationCardAggregate invitationCardAggregate = invitationService.queryInvitationCard(req.getInvitationId());
        // 分页查询
        PageBean<HandleInvitationRecordAggregate> res = invitationService.pageQueryInvitationRecord(req.getPageIndex(),req.getPageSize(),req.getInvitationId());
        res.getRecords().forEach(e->{
            e.setInvitationCardAggregate(invitationCardAggregate);
        });
        return res;
    }

    public void saveInvitationEntrust(InvitationEntrustEntity invitationEntrustEntity) {
        invitationRepository.saveInvitationEntrust(invitationEntrustEntity);
    }

    public PageBean<QueryInvitationEntrustRes> pageQueryInvitationEntrust(PageQueryInvitationEntrustReq req) {
        return invitationRepository.pageQueryInvitationEntrust(req);
    }
}
